<template>
	<view class="pages-technician"
		:style="{paddingTop:configInfo.plugAuth.industrytype == 3?'':'1rpx',background:pageColor}" v-if="isLoad">
		<block v-if="configInfo.plugAuth.industrytype == 3">
			<fixed>
				<tab @change="handerTabChange($event, 'activeIndex')" :list="careerList" :activeIndex="activeIndex*1"
					:activeColor="primaryColor" width="25%" height="90rpx" fontSize="28rpx"></tab>
			</fixed>
		</block>
		<view class="ml-md mr-md"
			:class="[{'flex-warp':configInfo.coach_list_format === 2 || careerList[activeIndex].type == 2}]"
			v-if="list.data && list.data.length>0">
			<view class="mt-md"
				:class="[{'mr-md':(configInfo.coach_list_format == 2 || careerList[activeIndex].type == 2) && index%2==0}]"
				v-for="(item,index) in list.data" :key="index">
				<longbingbing-technician-list-item @comment="toShowPopup(index,'message')" @collect="toCollect(index)"
					@order="toShowPopup(index,'technician')" :info="item" :type="careerList[activeIndex].type">
				</longbingbing-technician-list-item>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0&&location.lng" :loading="loading"
			v-if="loading">
		</load-more>

		<block v-if="!loading&&list.data.length<=0&&list.current_page==1">
			<abnor v-if="location.lat&&location.lng" type="USER" :isCenter="true">
			</abnor>
			<block v-if="!location.lat&&!location.lng">
				<!-- #ifdef H5 -->
				<abnor type="NOT_LOCATION" :title="`暂无收藏${$t('action.attendantName')}数据`"></abnor>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<abnor type="NOT_LOCATION" :title="`暂无收藏${$t('action.attendantName')}数据`" @confirm="toOpenLocation"
					:button="[{ text: '开启定位' , type: 'confirm' }]" btnSize=""></abnor>
				<!-- #endif -->
			</block>
		</block>

		<view class="space-footer"></view>

		<longbingbing-technician-list-popup ref="technician_list_popup"></longbingbing-technician-list-popup>

		<!-- #ifdef MP-WEIXIN -->
		<user-privacy ref="user_privacy" :show="false"></user-privacy>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<longbingbing-app-check-auth type="location" ref="app_check_item"
			@confirm="toConfirmOpenLoca"></longbingbing-app-check-auth>
		<!-- #endif -->

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import longbingbingTechnicianListItem from "@/components/longbingbing-technician-list-item.vue"
	import longbingbingTechnicianListPopup from "@/components/longbingbing-technician-list-popup.vue"
	export default {
		components: {
			longbingbingTechnicianListItem,
			longbingbingTechnicianListPopup
		},
		data() {
			return {
				isLoad: false,
				options: {},
				param: {
					page: 1,
					ser_id: 0,
					coach_name: ''
				},
				list: {
					data: []
				},
				loading: true,
				activeIndex: 0,
				careerList: [],
			}
		},
		computed: mapState({
			configInfo: state => state.config.configInfo
		}),
		onLoad() {
			this.initIndex()
		},
		async onShow() {
			if (!this.location.lat && this.locaRefuse) {
				this.toResetUtilLoca()
			}
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			this.getList();
		},
		watch: {
			locaRefuse(newval, oldval) {
				if (!newval) {
					this.toResetUtilLoca()
				}
			}
			// #ifndef H5
			,
			changeOnAddr(newval, oldval) {
				let {
					noloca,
				} = this.noChangeLoca
				if (newval && noloca) {
					this.initUtilLocaData()
				}
			},
			noChangeLoca(newval, oldval) {
				setTimeout(() => {
					let {
						lat,
						lng,
						unix = 0
					} = this.changeOnAddr
					let {
						noloca
					} = this.noChangeLoca
					let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
					if (noloca && ((!lat && !lng) || !unix || (unix && (cur_unix - unix >= 1)))) {
						this.getUtilLocation()
					}
				}, 800)
			}
			// #endif
		},
		methods: {
			...mapActions(['getMineInfo']),
			...mapMutations(['updateUserItem', 'updateMapItem']),
			async initIndex(refresh = false) {
				await this.getIndustryType()
				await this.isNotLoca()
				this.isLoad = true
				this.$util.setNavigationBarColor({
					bg: this.primaryColor
				})
				uni.setNavigationBarTitle({
					title: '收藏' + this.$t('action.attendantName')
				})
				// #ifdef H5
				if (!refresh) {
					this.$jweixin.hideOptionMenu()
				}
				// #endif
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex(true)
			},
			async toResetUtilLoca() {
				// #ifdef APP-PLUS
				let {
					lat: locaLat = 0
				} = this.location
				let {
					appCheckLocaStatus = null
				} = this
				if (!locaLat && appCheckLocaStatus == 1) {
					let {
						lng = 0,
							lat = 0
					} = await this.$util.getUtilLocation()
					if (!lat && !lng) return
					await this.initRefresh()
				}
				// #endif
			},
			async toOpenLocation() {
				// #ifdef APP-PLUS
				if (plus.os.name == 'Android' && plus.navigator.checkPermission(
						'android.permission.ACCESS_FINE_LOCATION') ===
					'undetermined') {
					this.$refs.app_check_item.open()
				} else {
					this.toConfirmOpenLoca()
				}
				// #endif
				// #ifndef APP-PLUS
				this.toConfirmOpenLoca()
				// #endif
			},
			async toConfirmOpenLoca() {
				// #ifdef MP-WEIXIN
				let privacyCheck = this.$refs.user_privacy.check()
				if (privacyCheck) {
					this.$refs.user_privacy.open()
					return
				}
				// #endif 
				this.initIndex()
			},
			toSearch(val) {
				this.param.page = 1
				this.param.coach_name = val
				this.getList()
			},
			// 获取职业
			async getIndustryType() {
				let data = await this.$api.service.industryTypeSelect()
				this.careerList = data
				let {
					industrytype
				} = this.configInfo.plugAuth
				if (industrytype != 3) {
					let ind = data.findIndex(item => {
						return item.type == industrytype
					})
					this.activeIndex = ind
				}
			},
			async isNotLoca() {
				let {
					location,
					locaRefuse
				} = this

				let {
					status: coach_status = 0,
					coach_position = 0
				} = this.userCoachStatus


				// #ifdef H5
				if (coach_position && coach_status == 2 && (location.lat && location.address != '暂未获取到位置信息')) {
					this.updateUserItem({
						key: 'isChangeCoachLoca',
						val: true
					})
				}
				// #endif


				// #ifdef APP-PLUS
				if (!locaRefuse && (!location.lat || (location.lat && location.address == '暂未获取到位置信息'))) {
					// #endif
					// #ifndef APP-PLUS
					if (!location.lat || (location.lat && location.address == '暂未获取到位置信息')) {
						// #endif

						// #ifndef H5
						if (coach_position && coach_status == 2) {
							let {
								lat: change_lat = 0,
								lng: change_lng = 0,
								unix = 0
							} = this.changeOnAddr
							let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(), 'YY-M-D h:m:s'))
							let noloca = change_lat && change_lng && (unix && (cur_unix - unix < 3)) ? false : true
							if (!noloca) {
								let loca = Object.assign({}, this.location, {
									lat: change_lat,
									lng: change_lng,
									is_util_loca: 1
								})
								this.updateUserItem({
									key: 'location',
									val: loca
								})
							}
							this.updateUserItem({
								key: 'noChangeLoca',
								val: {
									noloca
								}
							})
							if (this.noChangeLoca.noloca) return
							this.initUtilLocaData()
						} else {
							this.getUtilLocation()
						}
						this.updateMapItem({
							key: 'pageActive',
							val: false
						})
						return
						// #endif

						// #ifdef H5
						await this.$util.getUtilLocation()
						this.updateMapItem({
							key: 'pageActive',
							val: false
						})
						// #endif
					}
					this.initUtilLocaData()
				},
				async getUtilLocation() {
						await this.$util.getUtilLocation()
						this.initUtilLocaData()
					},
					async initUtilLocaData() {
							// #ifndef H5
							this.updateUserItem({
								key: 'noChangeLoca',
								val: {
									noloca: false
								}
							})
							// #endif 
							await this.getList()
							let {
								lat = 0, lng = 0, is_util_loca = 0, isIp = true
							} = this.location
							if (lat && lng && is_util_loca) {
								this.$util.getMapInfo(isIp)
							}
						},
						handerTabChange(index) {
							this.activeIndex = index
							this.param.page = 1
							this.list.data = []
							this.getList()
						},
						async getList() {
								let {
									list: oldList,
									param,
									careerList,
									activeIndex
								} = this
								let {
									lat = 0, lng = 0
								} = this.location
								param = Object.assign({}, param, {
									lat,
									lng,
								})
								let {
									industrytype
								} = this.configInfo.plugAuth
								if (industrytype == 3) {
									param.industry_type = careerList[activeIndex].id
								}
								let newList = await this.$api.mine.coachCollectList(param)
								newList.data.map(item => {
									item.is_collect = 1
								})
								if (this.param.page == 1) {
									this.list = newList
								} else {
									newList.data = oldList.data.concat(newList.data)
									this.list = newList
								}
								this.loading = false
								this.$util.hideAll()
							},
							async toShowPopup(index, key) {
									this.$refs.technician_list_popup.toShowPopup(this.list.data[index], key)
								},
								async toCollect(index) {
									let {
										id,
										is_collect,
										collect_num
									} = this.list.data[index]
									await this.$api.mine.delCollect({
										coach_id: id
									})
									this.$util.showToast({
										title: '取消成功'
									})
									this.list.data.splice(index, 1)
									this.getMineInfo()
								}
			}
		}
</script>


<style lang="scss">
	.pages-technician {}
</style>